/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.ticket;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.wechat.entity.ticket.TicketConfig;
import com.boxin.ims.modules.wechat.dao.ticket.TicketConfigDao;

/**
 * 入场券设置Service
 * @author Jakemanse
 * @version 2014-01-28
 */
@Component
@Transactional(readOnly = true)
public class TicketConfigService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(TicketConfigService.class);
	
	@Autowired
	private TicketConfigDao ticketConfigDao;
	
	public TicketConfig get(Long id) {
		return ticketConfigDao.findOne(id);
	}
	
	
	public TicketConfig getByUserId(Long userId){
		return ticketConfigDao.findByUserId(userId);
	}
	
	public Page<TicketConfig> find(Page<TicketConfig> page, TicketConfig ticketConfig) {
		DetachedCriteria dc = ticketConfigDao.createDetachedCriteria();
		if (ticketConfig.getUser()!=null && ticketConfig.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", ticketConfig.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(ticketConfig.getName())){
			dc.add(Restrictions.like("name", "%"+ticketConfig.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return ticketConfigDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(TicketConfig ticketConfig) {
		ticketConfigDao.save(ticketConfig);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		ticketConfigDao.deleteById(id);
	}
	
}
